/********************************************************************
 * PROJECT: CSAIR ROST PROJECT.
 * SUPPLIER: FOREVER OPEN SOURCE SOFTWARE INC.
 ********************************************************************
 * FILE: GanttGridColumn.java
 * (C) Copyright Foliage Team 2013, All Rights Reserved.
 ********************************************************************/
package org.foliage.rcp.ui.custom;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Item;
import org.eclipse.swt.widgets.Listener;
import org.foliage.rcp.ui.widgets.GridColumn;

/**
 * 带有报表功能的甘特图列控件。
 * <ul>
 * <li>桥接解耦报表列控件。</li>
 * </ul>
 * 
 * @author deathknight0718@gmail.com
 * @version 1.0.0
 */
public class GanttGridColumn extends Item {

	// ----------------------------------------------- 

	public static final String DATA_KEY = GanttGridColumn.class.getName();

	// ----------------------------------------------- 

	private final GridColumn column;

	private final GanttGrid parent;

	// ----------------------------------------------- 

	public GanttGridColumn(GanttGrid parent, int style) {
		this(parent, style, -1);
	}

	public GanttGridColumn(GanttGrid parent, int style, int index) {
		super(parent, style, index);
		this.parent = parent;
		this.column = new GridColumn(parent.getGrid(), style, index);
		this.column.setData(DATA_KEY, this);
		this.column.setMoveable(true);
		initListeners();
	}

	// ----------------------------------------------- 

	private void initListeners() {
		column.addListener(SWT.Move, new Listener() {

			@Override
			public void handleEvent(Event event) {
				parent.getJointHandler().jointColumMoving(event);
			}

		});
		column.addListener(SWT.Resize, new Listener() {

			@Override
			public void handleEvent(Event event) {
				parent.getJointHandler().jointColumnResize(event);
			}

		});
	}

	// ----------------------------------------------- 

	@Override
	public void addListener(int eventType, Listener listener) {
		column.addListener(eventType, listener);
	}

	@Override
	public Listener[] getListeners(int eventType) {
		return column.getListeners(eventType);
	}

	// ----------------------------------------------- 

	/**
	 * 列数据排序。
	 * <ul>
	 * <li>由于存在多图联动需求，因此此处将根据是否为多图联动而进行扩展。</li>
	 * </ul>
	 * 
	 * @param sortStyle 排序模式（SWT.UP、SWT.DOWN）。
	 */
	public void setSort(int sortStyle) {
		column.setSort(sortStyle);
		int index = getIndex();
		GanttGridItem[] items = parent.getItems();
		for (int i = 1; i < items.length; i++) {
			String text1 = items[i].getGridText(index);
			for (int j = 0; j < i; j++) {
				String text2 = items[j].getGridText(index);
				if (text1.compareTo(text2) > 0 && (sortStyle & SWT.DOWN) == SWT.DOWN) {
					GanttGridItemData data = items[i].getItemData();
					items[i].dispose();
					new GanttGridItem(parent, SWT.NONE, j, data);
					items = parent.getItems();
					break;
				}
				if (text2.compareTo(text1) > 0 && (sortStyle & SWT.UP) == SWT.UP) {
					GanttGridItemData data = items[i].getItemData();
					items[i].dispose();
					new GanttGridItem(parent, SWT.NONE, j, data);
					items = parent.getItems();
					break;
				}
			}
		}
	}

	/**
	 * 设置列筛选浮动框。
	 * <ul>
	 * <li>由于存在多图联动需求，因此此处将根据是否为多图联动而进行扩展。</li>
	 * </ul>
	 */
	public void setMenuEnabled() {
		addListener(SWT.Selection, new Listener() {

			@Override
			public void handleEvent(Event event) {
				GanttColumnMenu menu = new GanttColumnMenu(GanttGridColumn.this);
				menu.setItems(parent.getJointHandler().jointGettingItems());
				menu.open();
			}

		});
	}

	// ----------------------------------------------- 

	public int getWidth() {
		return column.getWidth();
	}

	public void setWidth(int width) {
		column.setWidth(width);
	}

	@Override
	public String getText() {
		return column.getText();
	}

	@Override
	public void setText(String text) {
		column.setText(text);
	}

	// ----------------------------------------------- 

	public Rectangle getBounds() {
		return column.getBounds();
	}

	public int getIndex() {
		return column.getParent().indexOf(column);
	}

	public int getHeaderHeight() {
		return column.getHeaderRenderer().getSize().y;
	}

	public GanttGrid getParent() {
		return parent;
	}

}
